#!/usr/bin/env perl

use strict;

BEGIN {
	if(!exists($::ENV{LINUXHA_LIBS})) {
		$::ENV{LINUXHA_LIBS}="/opt/linuxha14/lib/perl";
	}
	push @INC,$::ENV{LINUXHA_LIBS};
}

eval 'use libping6;';
if($@) {
	print STDERR "Failed to load libping6: $@\n"; exit(1);
}

print "Loaded libping6 ok...\n";

eval 'use cld_server;';
if($@) {
	print STDERR "Failed to load cld_server: $@\n"; exit(1);
}


my $serv=cld_server->new(PORT=>12345,KEY=>"mytestkey",TYPE=>6);
if(!defined($serv)) {
	print STTDERR "Failed to open server on port 12345\n"; exit(1);
}

while(1) {
	my $sock=$serv->accept();
	if(!defined($sock)) {
		select(undef,undef,undef,0.1);
		next;
	}
	my $msg=$serv->decode_msg();
	if(!defined($msg)) {
		my ($a,$b)=$serv->get_error();
		print STDERR "Error: $a [rc=$b]\n";
	} else {
		print "Recieved: $msg\n";
		my $failed=$serv->encode_msg(MSG=>"Hello: $msg");
		if($msg =~ /^QUIT/) {
			exit(0);
		}
	}
	$serv->close_session();
}

